#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "surface.h"
#include <time.h>
using namespace std;

char *surface::display_cache(){
    return _cache;
}
void surface::update_info(chess_info* info){}

void surface::display(){
    system("clear");
    remove_null_char();
    for(int i=0; i<x; i++){
        cout<<&_cache[i*y]<<endl;
    }
    /*
    cout<<"cache:"<<endl;
    for(int i=0; i<x*y; i++){
        cout<<_cache[i];
    }  
    cout<<endl; //*/
}
void surface::add_surface(int x,int y,int pos_x,int pos_y,char *surface){
    for(int i=0; i<x; i++){
        strcpy(_cache+(pos_x+i)*this->y+pos_y,&surface[i*y]);
    }
}
void surface::remove_null_char(){
    for(int i=0; i<x; i++){
        for(int j=0; j<y-1; j++){
            if(_cache[i*y+j] == '\0'){
                _cache[i*y+j] = default_char;
            }
        }
        _cache[i*y+y-1] = '\0';
    }
}

surface::surface(int x,int y,char default_char):x(x),y(y),default_char(default_char){
    _cache = new char[x*y]; 
    for(int i=0; i<x; i++){
        for(int j=0; j<y; j++){
            if(j != y-1){ _cache[i*y+j] = default_char; }
            else{ _cache[i*y+j] = '\0'; }
        }
    }
}
surface::~surface(){
    delete [] _cache;
}

// chess board
char *surface_chessboard::display_cache(){
    _cache[current_chess_x*y+current_chess_y] = current_chess;
	return _cache;
}
void surface_chessboard::update_info(chess_info *info){
    current_chess = info->current_chess;
    current_chess_x = info->current_x;
    current_chess_y = info->current_y;
}
int surface_chessboard::is_five_in_row(){
    char chess;
    for(int i=0; i<2; i++){
        chess = all_chess[i];
        for(int m=0; m<x; m++){
            for(int n=0; n<y; n++){
                if(_cache[m*y+n] == chess){
                    if(is_five_in_row_for_single_chess(m,n,chess)==1){
                        return i;
                    }
                }
            }
        }
    }
    return -1;
}
int surface_chessboard::is_five_in_row_for_single_chess(int m,int n,char chess){
    int directions[8][2] = {{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
    int line_length;
    int tmp_m,tmp_n;
    for(int i=0; i<8; i++){
        tmp_m = m;
        tmp_n = n;
        line_length = 1;
        while(_cache[tmp_m*y+tmp_n] == chess){
            tmp_m += directions[i][0];
            tmp_n += directions[i][1];
            line_length ++;
        }
        if(line_length>5){
            return 1;
        }
    }
    return 0;
}

// chess info
surface_infoboard::surface_infoboard(int x,int y):surface(x,y){
    start_time = time(nullptr);
    this_step_start_time = time(nullptr);
}
char *surface_infoboard::display_cache(){
    strcpy(_cache, "now:");
    time_t lt = time(nullptr);
    strcpy(&_cache[y], ctime(&lt));
    strcpy(&_cache[2*y], "duration time:");
    char tmp[y];
    sprintf(tmp,"%f s", lt - start_time);
    strcpy(&_cache[3*y],tmp); 
    strcpy(&_cache[4*y], "time for this player:");
    sprintf(tmp, "%f s", lt - this_step_start_time);
    strcpy(&_cache[5*y],tmp);
    strcpy(&_cache[6*y], "who to play now:");
    strcpy(&_cache[7*y], &who_play_this_step);
    
    return _cache;
}
void surface_infoboard::update_info(chess_info *info){
    who_play_this_step = info->current_chess;
    this_step_start_time = time(nullptr);    
}
